package edu.scsu.csci.validator;

import java.lang.annotation.Annotation;

import edu.scsu.csci.IValidator;
import edu.scsu.csci.annotation.validator.AFloatValidator;


/**
 * Validates the any primitive or simple float's based on the validation 
 * specified by AIntegerValidator.
 * 
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 */
final public class FloatValidator implements IValidator<AFloatValidator, Float>{
	
	/**
	 * <p>Validates the passed Float object based on the validator annotaion passesd.
	 * 
	 * @param annotation
	 * @param object
	 * @throws ValidationException 
	 */
	public void validate(AFloatValidator annotation, Float value) throws ValidationException {
		if( isNull(annotation, value)){
			throw new ValidationException("field cannot be null"); 
		}
		if(isIllegalValue(annotation, value)){
			throw new ValidationException(annotation.errorMessage());
		}
	}
	
	/**
	 * Returns true if the null is not an acceptable value
	 *  and the passed float object is null. 
	 * 
	 * @param annotation
	 * @param float
	 * @return
	 */
	private boolean isNull(AFloatValidator annotation, Float value){
		return annotation.isNullAllowed() ? false : value == null;
	}
	
	/**
	 * Returns true if the string of any other length than specified by the
	 * validator annotation.
	 * 
	 * @param annotation
	 * @param string
	 * @return
	 */
	private boolean isIllegalValue(AFloatValidator annotation, Float value){
		return value.floatValue() < annotation.min() || 
					value.floatValue() > annotation.max(); 
	}

	/**
	 * Retunrs true is can vlaidation can be performed for the passed object using the passed annotation.
	 * 
	 * @param type
	 * @param annotaionType
	 * @return
	 */
	public boolean canValidate(Class<?> type, Class<? extends Annotation> annotaionType) {
		if((type.getClass().equals(float.class) || type.getClass().equals(Float.class)) 
					&& AFloatValidator.class.equals(annotaionType)){
			return true;
		}
		return false;
	}
}
